package com.shijunhao.graduation.controller.system;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.domain.dto.SystemDictItemQueryDto;
import com.shijunhao.graduation.domain.dto.SystemDictQueryDto;
import com.shijunhao.graduation.domain.entity.SystemDict;
import com.shijunhao.graduation.domain.entity.SystemDictItem;
import com.shijunhao.graduation.service.system.ISystemDictItemService;
import com.shijunhao.graduation.utils.PageUtils;
import com.shijunhao.graduation.utils.WrapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shijunhao
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/system-dict-item")
@Api(tags = "字典项管理")
public class SystemDictItemController {

    private final ISystemDictItemService systemDictItemService;

    @Autowired
    public SystemDictItemController(ISystemDictItemService systemDictItemService) {
        this.systemDictItemService = systemDictItemService;
    }

    @GetMapping("/list")
    @ApiOperation("字典项列表")
    public ResultCode<IPage<SystemDictItem>> list(@ModelAttribute SystemDictItemQueryDto query) {
        IPage<SystemDictItem> info = systemDictItemService.page(PageUtils.getPage(), WrapperUtils.query(query));
        return ResultCode.success(info);
    }

    @PostMapping("insert")
    @ApiOperation("增加字典项")
    public ResultCode<Long> insert(@RequestBody SystemDictItem entity){
        boolean b = systemDictItemService.insertOne(entity);
        if(b)
            return ResultCode.success("插入成功",entity.getId());
        else
            return ResultCode.error("插入失败");
    }

    @PostMapping("update")
    @ApiOperation("更新字典项")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResultCode<Boolean> update(@RequestBody SystemDictItem entity){
        boolean b = systemDictItemService.updateById(entity);
        if(b)
            return ResultCode.success("更新成功");
        else
            return ResultCode.error("插入失败");
    }

    @PostMapping("delete/{id}")
    @ApiOperation("删除字典项")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResultCode<Boolean> delete(@PathVariable Long id){
        boolean b = systemDictItemService.deleteById(id);
        if(b)
            return ResultCode.success("删除成功");
        else
            return ResultCode.error("删除失败");
    }
}
